package com.plankton

import com.alibaba.fastjson.JSON
import android.util.Base64
import com.jsdroid.commons.HttpUtil
import com.jsdroid.commons.ShellUtil
import com.jsdroid.script.JsDroidScript
import org.jsoup.Connection
import org.jsoup.Jsoup

/**
 * 痞老板插件
 */

class PiLaoBan {
    static def exec(String command) {
        ShellUtil.exec(command)
    }

    static def 安装APP(def 安装包路径) { exec("pm install -r ${安装包路径}") }

    static def 卸载APP(def APP包名) { exec("pm uninstall  ${APP包名}") }

    static def 获取设备中的应用() {
        exec("pm list packages -3");
    }



    static def wifi是否开启() {
        def text = exec("dumpsys wifi")
        def reg = (text =~ /Wi-Fi is ([\s\S]*?)\nStay-awake/);
        reg.find()
        reg.group(0)
    }

    static def 获取安卓版本号() {
        exec("getprop ro.build.version.release")
    }

    static def 重启手机() { exec("reboot") }

    static def 关机() { exec("reboot -p") }

    static def 微信浏览器打开网页(def 包名, def 网址) { exec("am start -n ${包名}/.plugin.webview.ui.tools.WebViewUI -d '${网址}'") }

    static def 默认浏览器打开网址(def url) {//需要完整的网址，http：//www.baidu.com
        exec("am start -a android.intent.action.VIEW -d ${url}")
    }


    static def 清空应用缓存(def 包名) { exec("pm clear ${包名}") }

    static def 获取系统上输入法() { exec("ime list -s") }
    static def 获取包名和组件名(){
        def text=exec("dumpsys activity top")
        def reg = (text =~ /ACTIVITY ([^ ]+)/);
        if(reg.find()){
            def a;
            return reg.group(0)
        }else{
            return reg.group(0)
        }
    }
}
class BaiDuYun{
    static def Base64Encode(String filePath){
        InputStream inputStream = new FileInputStream(filePath);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        byte[] bytes = Base64.encode(out.toByteArray(),Base64.NO_WRAP)
        return new String(bytes,"UTF-8")
    }

    static def 获取access_token(String AK, String SK){
   def json= JSON.parse(HttpUtil.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${AK}&client_secret=${SK}"))
     return json.access_token

    }
    static def 通用文字识别(String access_token,String 路径){
        def req= HttpUtil.post("https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=${access_token}",[image:Base64Encode(路径)])
    return req
    }
    static def 通用文字识别_高精度版(String access_token,String 路径){
        def req= HttpUtil.post("https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 通用文字识别_含位置信息版(String access_token,String 路径){
        def req= HttpUtil.post("https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 通用文字识别_高精度含位置版(String access_token,String 路径){
        def req= HttpUtil.post("https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 通用文字识别_含生僻字版(String access_token,String 路径){
        def req= HttpUtil.post("https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 手写文字识别(String access_token,String 路径){
        def req= HttpUtil.post("https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 身份证识别(String access_token,String 路径){
        def req= HttpUtil.post("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 银行卡识别(String access_token,String 路径){
        def req= HttpUtil.post("https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 营业执照识别(String access_token,String 路径){
        def url="https://aip.baidubce.com/rest/2.0/ocr/v1/business_license"
        def req= HttpUtil.post("${url}?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 护照识别(String access_token,String 路径){
        def url="https://aip.baidubce.com/rest/2.0/ocr/v1/passport"
        def req= HttpUtil.post("${url}?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 名片识别(String access_token,String 路径){
        def url="https://aip.baidubce.com/rest/2.0/ocr/v1/business_card"
        def req= HttpUtil.post("${url}?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 户口本识别(String access_token,String 路径){
        def url="https://aip.baidubce.com/rest/2.0/ocr/v1/household_register"
        def req= HttpUtil.post("${url}?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }
    static def 自定义接口识别(String url,String access_token,String 路径){
        def req= HttpUtil.post("${url}?access_token=${access_token}",[image:Base64Encode(路径)])
        return req
    }

}
class Codec{
    static def Base64Encode(String filePath){ //把文件转换成base64
        InputStream inputStream = new FileInputStream(filePath);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        byte[] bytes = Base64.encode(out.toByteArray(),Base64.NO_WRAP)
        return new String(bytes,"UTF-8")
    }

    static String URLDecoderString(String str) { //URL编码方法 --- 解码
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    static String URLEncodeString(String str) { //URL编码方法 --- 编码
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;

    }





}
class HttpEX{
    static def get(url,header,cookie){
        try {
           def response= Jsoup.connect(url)
                    .ignoreContentType(true)//忽略类型错误
                    .ignoreHttpErrors(true)//忽略http错误
                    .validateTLSCertificates(false)//不校验证书
                    .method(Connection.Method.GET)//访问方式 POST.
                    .headers(header)//添加协议头
                    .cookies(cookie)//添加cookies
                    .execute()
            return response.body()
        }catch(e){
          print(e)
        }
    }
    static def post(url,post_data,header,cookie){
        try {
            def response= Jsoup.connect(url)
                    .ignoreContentType(true)//忽略类型错误
                    .ignoreHttpErrors(true)//忽略http错误
                    .validateTLSCertificates(false)//不校验证书
                    .method(Connection.Method.POST)//访问方式 POST.
                    .requestBody(post_data)
                    .headers(header)//添加协议头
                    .cookies(cookie)//添加cookies
                    .execute()
            return response.body()
        }catch(e){
            print(e)
        }
    }

    static void download(String url, String file) { //下载文件
        HttpUtil.download(url, file);
    }

    static def upload(url,file_header=[],file_type="",filename="",file_local="",name="",timeout=5000){ //标准网页表单格式上传文件
        try {
            Connection.Response response = Jsoup.connect(url)
                    .ignoreHttpErrors(true)
                    .ignoreContentType(true)
                    .validateTLSCertificates(false)
                    .method(Connection.Method.POST)
                    .timeout(timeout)
                    .followRedirects(false)
                    .data(file_header)
                    .data(name, filename, new FileInputStream(file_local),file_type)
                    .execute();
            return response.body()
        }catch(e){
            JsDroidScript.print(e)
        }
    }

}

